'''
Created on Aug 5, 2011

@author: bstar
'''
import ast
from dopplershift.core.plugins.factory import PluginsFactory
from dopplershift.core.conf.pair import XMLPairConfig
from dopplershift.core.app.cline import CommandLineApp
from dopplershift.core.containers.mediator import getMediator
from twisted.internet import reactor
from dopplershift.core.utils.logger import DSLogging
from dopplershift.core.events import DSEventManager, mq, txsystem

class DSBackendBuilder(CommandLineApp):
    config_class = XMLPairConfig
    __arguments__ = (
                     {
                      'name': '--port',
                      'dest': 'port',
                      'type': int,
                      'help': 'service port number'
                      },
                     )
    def __init__(self):
        ds = DSLogging("DEBUG")
        ds.twistedObserver()
        CommandLineApp.__init__(self)
        self._port = self._args.port
        config = self.config_class(self._args.configfile)
        mediator = getMediator()
        mediator.register("AppConfig", config)
        mediator.register("EventManager", DSEventManager([
                                                          mq.EVENT_MQ_MESSAGE_RECEIVED, 
                                                          mq.EVENT_MQ_MESSAGE_SEND, 
                                                          txsystem.EVENT_REACTOR_STARTED, 
                                                          txsystem.EVENT_REACTOR_STOPPED
                                                          ]))
        pf = PluginsFactory()
        mediator.register('PluginsFactory', pf)
        pf.configure(config)
        pf.build('backend.load')
    def run(self):
        reactor.run()